import _ from "lodash"

export default class Inventories {

  constructor(Vue, storeId) {
    this.VUE = Vue;
    this.storeId = storeId;
    this.categoriesPromise = this._getCategories();
    this.inventoriesPromise = this._getInventories();
  }

  //获取所有商品
  _getInventories() {
    return Promise.resolve(this.VUE.$http.get('/api/stores/' + this.storeId +
      '/inventories/all/')).then(
      response => response.data,
      response => {
        console.log("http error");
      }
    );
  }

  //获取所有分类
  _getCategories() {
    return Promise.resolve(this.VUE.$http.get('/api/stores/' + this.storeId +
      '/categories')).then(
      response => {
        return response.data;
      },
      response => {
        console.log("http error");
      }
    );
  }

  getCategories() {
    return this.categoriesPromise;
  }

  getInventories() {
    return this.inventoriesPromise;
  }

  static getModifiersByInventoryId(inventories, cartItem) {
    let arr = inventories[cartItem.categories[0].id];
    for (let i = 0, l = arr.length; i < arr.length; i++) {
      if (arr[i].id == cartItem.inventoryId) {
        return arr[i].modifiers;
      }
    }
  }

  getInventoriesGroupByCategories() {
    // *getInventoriesGroupByCategories() {
    //   let [inventories, categories] = yield [this.inventoriesPromise, this.categoriesPromise];
    return Promise.all([this.inventoriesPromise, this.categoriesPromise]).then(
      ([inventories, categories]) => {
        let res = {};
        //所有的modifiers 排序
        inventories.forEach(inventory => {
          inventory.modifiers.length > 0 && inventory.modifiers.sort(
            function(a, b) {
              if (!b.isRequired) {
                return false;
              }
              return true;
            })
        })
        Inventories.originalInventories = inventories;
        if (inventories && categories) {
          categories.forEach((ele, i) => {
            res[ele.id] = [];
            inventories.forEach((element, i) => {
              element.categories.forEach((e) => {
                if (ele.id == e.id) {
                  res[ele.id].push(element);
                }
              });
            });
          });

          res['nocategory'] = inventories.filter((elem) => {
            return elem.categories.length === 0
          });
        }
        if (categories.length === 0) {
          categories.push({
            "name": "所有",
            "id": "nocategory",
            "showOrder": 99
          });
        } else if (res['nocategory'].length != 0) {
          categories.push({
            "name": "其他",
            "id": "nocategory",
            "showOrder": 99
          });
        }
        return res;
      })
  }

  //商品是否有必选项
  static isInventoryIsRequired(inventory) {
    let res = false;
    if (inventory.modifiers && inventory.modifiers.length) {
      for (let i = 0, l = inventory.modifiers.length; i < l; i++) {
        if (inventory.modifiers[i] && inventory.modifiers[i].isRequired) {
          res = true;
          break;
        }
      }
    }
    return res;
  }

  static getIvtsByIds(ids) {

    return new Promise(function(resolve, reject) {
      let timer = setInterval(function() {
        if (Inventories.originalInventories.length > 0) {

          clearInterval(timer);
          const originalInventories = Inventories.originalInventories;
          let res = {};
          let ivtidArr = [];
          let itemsL = ids.length;
          let count = 0;
          let existInvts = Inventories.existInvts;
          ids.forEach(inventoryId => {
            if (existInvts[inventoryId]) {
              res[inventoryId] = existInvts[inventoryId];
              count++;
            } else {
              ivtidArr.push(inventoryId);
            }
          })
          for (let i = 0, l = originalInventories.length; i < l; i++) {
            const inventory = originalInventories[i];
            const inventoryId = inventory.id;
            const index = ivtidArr.indexOf(inventoryId);
            if (index > -1) {
              res[inventoryId] = inventory;
              count++;
            }
            if (count == itemsL) {
              break;
            }
          }
          resolve(res);
        }
      }, 100);
    })
  }
}

//不能使用static 写在类里面
Inventories.existInvts = {}
Inventories.originalInventories = {}
